import datetime

from sqlalchemy import Column, Integer, String, Float, DateTime, UniqueConstraint

from QualityInspectionViolation.DbInit.mysqlDb import Base, session
from QualityInspectionViolation.Model.Adviser import Adviser
from QualityInspectionViolation.Model.RoleMenu import RoleMenu


class Menu(Base):
    __tablename__ = 'menu'
    id = Column(Integer, primary_key=True)
    systemId = Column(Integer)
    parentMenuId = Column(Integer)
    type = Column(Integer)
    name = Column(String(32))
    createdAt = Column(DateTime, default=datetime.datetime.now)
    updatedAt = Column(DateTime, default=datetime.datetime.now)

    def queryMenuByAdviser(self, adviserId):
        menuList = session.query(Menu).join(RoleMenu, RoleMenu.menuId == Menu.id).join(Adviser, Adviser.roleId == RoleMenu.roleId).filter(Adviser.id == adviserId).all()
        # menuList = session.execute('select m.* from menu m inner join role_menu rm on m.id = rm.menuId inner join adviser a on a.roleId = rm.roleId where a.id = :adviserId',params={"adviserId": adviserId}).fetchall()
        return menuList

    def deleteMenu(self, systemId, session):
        session.query(Menu).filter(Menu.systemId == systemId).delete()

    def insertMenu(self, Menu, session):
        session.add(Menu)